﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: WebResource("OpenWaves.Video.Web.Resources.jquery.video.js", "text/javascript")]
namespace OpenWaves.Video.Web.Controls
{
    public class Video : Control
    {
        public string VideoUrl
        {
            get { return (string) this.ViewState["VideoUrl"] ?? string.Empty; }
            set { this.ViewState["VideoUrl"] = value; }
        }

        public string CssClass
        {
            get { return (string)this.ViewState["CssClass"] ?? string.Empty; }
            set { this.ViewState["CssClass"] = value; }
        }

        public int Width
        {
            get { return (int) (this.ViewState["Width"] ?? 0); }
            set { this.ViewState["Width"] = value; }
        }

        public int Height
        {
            get { return (int) (this.ViewState["Height"] ?? 0); }
            set { this.ViewState["Height"] = value; }
        }

        public bool IncludeVideoPluginScript
        {
            get { return (bool) (this.ViewState["IncludeVideoPluginScript"] ?? true); }
            set { this.ViewState["IncludeVideoPluginScript"] = value; }
        }

        public bool EnableClientSideApis
        {
            get { return (bool)(this.ViewState["EnableClientSideApis"] ?? false); }
            set { this.ViewState["EnableClientSideApis"] = value; }
        }

        protected override void CreateChildControls()
        {
            
            var videoInfoProvider = ServiceLocator.Resolve<IVideoWebsiteApi>();
            var videoInfo = videoInfoProvider.GetVideo(this.VideoUrl);

            if (videoInfo != null)
            {
                var control = ServiceLocator.Resolve<IVideoEmbedControlFactory>().CreateEmbedControl(videoInfo, ClientID, EnableClientSideApis);
                
                if(this.CssClass != string.Empty)
                {
                    control.Attributes.Add("class", this.CssClass);
                }

                control.Attributes.Add("height", new Unit(this.Height, UnitType.Pixel).ToString(CultureInfo.InvariantCulture));
                control.Attributes.Add("width", new Unit(this.Width, UnitType.Pixel).ToString(CultureInfo.InvariantCulture));
                this.Controls.Add(control);
                base.CreateChildControls();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (IncludeVideoPluginScript)
            {
                Page.ClientScript.RegisterClientScriptInclude(typeof(VideoEmbedControlFactory), "jquery.video.js", ResolveClientUrl("~/Scripts/jquery.video.js"));
            }
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            this.EnsureChildControls();
            base.RenderControl(writer);
        }


        protected new virtual void EnsureChildControls()
        {
           if (!this.ChildControlsCreated)
           {
               try 
               {
                  this.CreateChildControls();
               }
               finally 
               {
                  this.ChildControlsCreated = true;
               }
           }
        }
    }
}